package site.lanmushan.quiz.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import site.lanmushan.common.dto.Message;
import org.springframework.web.bind.annotation.*;
import site.lanmushan.common.dto.QueryInfo;
import site.lanmushan.common.query.annotations.RequestQueryInfo;
import site.lanmushan.common.query.controller.BaseController;
import site.lanmushan.quiz.api.ao.QuizTbBankExamRecordAo;
import site.lanmushan.quiz.api.ao.StartQuizTbBankExamAo;
import site.lanmushan.quiz.api.entity.QuizTbBankExamRecord;
import site.lanmushan.quiz.api.service.IQuizTbBankExamRecordService;

import java.util.List;
import javax.validation.Valid;

import site.lanmushan.common.bizlog.annotations.BizLog;
import site.lanmushan.common.bizlog.consts.BizLogType;

/**
 * @author $author
 * 题库测验记录
 */
@RestController
@RequestMapping(name = "题库测验记录", value = "/QuizTbBankTestRecord")
@Slf4j
public class QuizTbBankTestRecordController extends BaseController {
    @Autowired
    IQuizTbBankExamRecordService IQuizTbBankExamRecordService;

    @BizLog(name = "题库测验记录-分页条件查询", bizType = BizLogType.SELECT)
    @GetMapping(name = "分页条件查询", value = "/selectPageList")
    public Message<List<QuizTbBankExamRecord>> selectPageList(@RequestQueryInfo QueryInfo queryInfo) {
        List<QuizTbBankExamRecord> quizTbBankExamRecordList = IQuizTbBankExamRecordService.selectPageList(queryInfo);
        return Message.getInstance().setData(quizTbBankExamRecordList).success("查询成功");
    }

    @GetMapping(name = "查询当前用户正在测试的题库", value = "/selectCurrentUserAnsweringExam")
    public Message<QuizTbBankExamRecord> selectCurrentUserAnsweringExam(@RequestParam("bankCode") String bankCode) {
        QuizTbBankExamRecord quizTbBankExamRecord = IQuizTbBankExamRecordService.selectCurrentUserAnsweringExam(bankCode);
        return Message.getInstance().setData(quizTbBankExamRecord).success("查询成功");
    }

    @BizLog(name = "题库测验记录-根据ID查询", bizType = BizLogType.SELECT)
    @GetMapping(name = "根据ID查询", value = "/selectById")
    public Message<QuizTbBankExamRecord> selectById(@RequestParam Long id) {
        QuizTbBankExamRecord resultEntity = IQuizTbBankExamRecordService.selectById(id);
        return Message.getInstance().setData(resultEntity).success("查询成功");
    }

    @BizLog(name = "题库测验记录-新增", bizType = BizLogType.INSERT)
    @PostMapping(name = "新增", value = "/save")
    public Message<Long> saveEntity(@RequestBody @Valid QuizTbBankExamRecordAo entityAo) {
        IQuizTbBankExamRecordService.insertService(entityAo);
        return Message.getInstance().setData(entityAo.getId()).success();
    }

    @BizLog(name = "题库测验记录-批量新增", bizType = BizLogType.INSERT)
    @PostMapping(name = "批量新增", value = "/saveList")
    public Message<List<Long>> saveEntityList(@RequestBody @Valid List<QuizTbBankExamRecordAo> entityAos) {
        List<Long> ids = IQuizTbBankExamRecordService.insertList(entityAos);
        return Message.getInstance().setData(ids).success();
    }

    @BizLog(name = "题库测验记录-更新", bizType = BizLogType.UPDATE)
    @PostMapping(name = "更新", value = "/update")
    public Message<Boolean> updateEntity(@RequestBody @Valid QuizTbBankExamRecordAo entityAo) {
        IQuizTbBankExamRecordService.updateService(entityAo);
        return Message.getInstance().success();
    }

    @BizLog(name = "题库测验记录-批量更新", bizType = BizLogType.UPDATE)
    @PostMapping(name = "批量更新", value = "/updateList")
    public Message<Integer> updateEntityList(@RequestBody @Valid List<QuizTbBankExamRecordAo> entityAos) {
        Integer count = IQuizTbBankExamRecordService.updateListBySelective(entityAos);
        return Message.getInstance().setData(count).success();
    }

    @BizLog(name = "题库测验记录-逻辑删除", bizType = BizLogType.DELETE)
    @PostMapping(name = "逻辑删除", value = "/logicDelete")
    public Message<Boolean> logicDelete(@RequestBody List<Long> ids) {
        IQuizTbBankExamRecordService.logicDeleteByIds(ids);
        return Message.getInstance().success();
    }

    @BizLog(name = "题库测验记录-物理删除", bizType = BizLogType.DELETE)
    @PostMapping(name = "物理删除", value = "/delete")
    public Message<Boolean> delete(@RequestBody List<Long> ids) {
        IQuizTbBankExamRecordService.deleteServiceByIds(ids);
        return Message.getInstance().success();
    }

    @PostMapping(name = "开始测验", value = "/startExam")
    public Message<QuizTbBankExamRecord> startExam(@RequestBody StartQuizTbBankExamAo startQuizTbBankExamAo) {

        QuizTbBankExamRecord record = IQuizTbBankExamRecordService.startExam(startQuizTbBankExamAo);
        return Message.getInstance().setData(record).success();
    }

}
